package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.ApAuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements AuthorControllerApi {
    @Resource
    ApAuthorService apAuthorService;

    /**
     * 根据用户id查询作者信息
     *
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/findByUserId/{userId}")
    public ApAuthor findByUserId(@PathVariable("userId") Integer userId) {
        return apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {

        apAuthorService.save(apAuthor);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    @GetMapping("/findByWmUserId/{wmUserId}")
    public ApAuthor findByWmUserId(@PathVariable("wmUserId") Integer wmUserId) {
        return apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, wmUserId));
    }


    @Override
    @GetMapping("/one/{id}")
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return apAuthorService.getById(id);
    }
}
